package com.ohc.leetcode.数组.repect;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * 2022年4月28日23:20:58
 */
public class 两个数组的交集01 {
    public static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for (int i : nums1) {
            set.add(i);
        }
        for (int i : nums2) {
            if (set.contains(i)) {
                set2.add(i);

            }
        }
        int[] arr = new int[set2.size()];
        Iterator<Integer> iterator = set2.iterator();
        int n = 0;
        while (iterator.hasNext()) {
            arr[n] = iterator.next();
        }

        return arr;
    }


    public static void main(String[] args) {
        int[] nums1 = new int[]{1, 2, 2, 1};
        int[] nums2 = new int[]{2, 2};
        int[] intersection = intersection(nums1, nums2);
        for (int i : intersection) {
            System.out.println("i = " + i);
        }

    }
}
